define('appRouter', ['vue', 'vue-router', 'iview','WxCore','restTemplate', 'appRoutes', 'appStore','siteConfig','TokenStore','AuthorizingRealm','resolveComponent'], function (
    Vue,
    VueRouter,
    iview,
    wxCore,
    restTemplate,
    appRoutes,
    appStore,
    siteConfig,
    tokenStore,
    authorizingRealm,
    resolveComponent
) {
    Vue.use(VueRouter)

    // 处理模块懒加载
    const loadStoreModules=function (to,next) {
        if (to.meta.storeModules && to.meta.storeModules.length > 0) {
            // 从路由元数据中取待加载模块
            let checkPromies = to.meta.storeModules.map((item, index) => {
                let storeModule = {
                    name: item,
                    storeModule: () => {
                        return resolveComponent('store/'+item);
                    }
                }
                //console.log(storeModule);
                return appStore.dispatch('findModule', storeModule)
            })
            // 检查待加载模块，过滤去已加载模块
            Promise.all(checkPromies).then(moduleLoadStates => {
                return new Promise((resolve, reject) => {
                    let waitLoadModules = moduleLoadStates.filter(moduleLoadState => !moduleLoadState.loaded)
                    resolve(waitLoadModules)
                })
            }, error => console.error(error)).then(waitLoadModules => {
                // 构建待加载的promise集合，共同发起处理
                let loadPromises = waitLoadModules.map((item, index) => {
                    return item.storeModule
                })
                return Promise.all(loadPromises)
            }).then(modules => {
                // 全部加载后，注册模块，记录加载状态
                modules.forEach((item, index) => {
                    //console.log(item);
                    if (item.storeName) {
                        appStore.registerModule(item.storeName, item, {
                            preserveState: true,
                            runInNewContext: 'once'
                        })
                        appStore.dispatch('addLoadedModule', item.storeName)
                        // console.log(store)
                    } else {
                        console.error('模块的StoreName未定义')
                    }
                })
                next()
            })
        } else {
            next()
        }
    }

    const pageRouter = new VueRouter({
        routes: appRoutes,
        mode: 'hash'
    })

    pageRouter.beforeEach((to, from, next) => {
        const token = tokenStore.getToken();
        if(!tokenStore.exists()){
            authorizingRealm.unLoginHandler(to,from,next);
        }else {
            appStore.commit('setBreadCrumb', to.matched)
            iview.LoadingBar.start()

            //加载用户信息
            const userinfo = wxCore.getSessionStoreItem('userinfo');
            if(!userinfo || userinfo=='' || userinfo=='undefined'){
                restTemplate.request({url:'/client/userinfo'}).then(res =>{
                    //console.info(res);
                    wxCore.setSessionStoreItem('userinfo',res);
                    appStore.commit('setUserInfo', res);
                    loadStoreModules(to,next);
                });
            }else {
                appStore.dispatch('getUserId').then(userId => {
                    if(!userId || userId == ''){
                        appStore.commit('setUserInfo', wxCore.getSessionStoreItem('userinfo'));
                    }
                    loadStoreModules(to,next);
                });
            }
            //console.log(tokenStore.getToken());
            //next()

        }

    })
    pageRouter.afterEach(to => {
        iview.LoadingBar.finish()
        window.scrollTo(0, 0)
    })

    return pageRouter
})
